package cn.sima.plantmessage.fragment;


import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;

import cn.sima.plantmessage.Conf;
import cn.sima.plantmessage.MainActivity;
import cn.sima.plantmessage.R;
import cn.sima.plantmessage.cache.ACache;
import net.tsz.afinal.FinalDb;
import cn.sima.plantmessage.user.UpdateUserinfoActivity;
import cn.sima.plantmessage.user.UploadUserHeadActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


public class UserinfoFragment extends Fragment implements OnClickListener{
	private ImageView user_info_headimage;
	private ImageView user_info_headimage1;
	private TextView user_info_username;
	private TextView user_info_sex;
	private TextView user_info_address;
	private TextView user_info_email;
	private TextView user_info_phone;
	private TextView user_info_userage;
	private TextView user_info_signtext;
	private Button loginout_user;
	FinalDb db;
	private ACache userCache;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_userinfo, container, false);
		initView(view);
		return view;
	}


	private void initView(View view) {
		userCache = ACache.get(getActivity().getApplicationContext(), "userCache");
		user_info_headimage = (ImageView)view.findViewById(R.id.user_info_headimage);
		user_info_headimage1 = (ImageView)view.findViewById(R.id.user_info_headimage1);
		user_info_username = (TextView)view.findViewById(R.id.user_info_username);
		user_info_sex = (TextView)view.findViewById(R.id.user_info_sex);
		user_info_userage = (TextView)view.findViewById(R.id.user_info_userage);
		user_info_email = (TextView)view.findViewById(R.id.user_info_email);
		user_info_phone = (TextView)view.findViewById(R.id.user_info_phone);
		user_info_signtext = (TextView)view.findViewById(R.id.user_info_signtext);
		user_info_address = (TextView)view.findViewById(R.id.user_info_address);
		loginout_user = (Button)view.findViewById(R.id.loginout_user);
		
		
		String userinfo = userCache.getAsString("user");
		JsonObject userinfoJson =  new JsonParser().parse(userinfo).getAsJsonObject();
		
		
		
		user_info_username.setText(userinfoJson.get("name").getAsString());
		user_info_address.setText(userinfoJson.get("address").getAsString());
		user_info_sex.setText(userinfoJson.get("sex").getAsString());
		
		
		user_info_userage.setText(userinfoJson.get("age").getAsString());
		user_info_email.setText(userinfoJson.get("email").getAsString());
		user_info_phone.setText(userinfoJson.get("phoneNumber").getAsString());
		user_info_signtext.setText(userinfoJson.get("signtext").getAsString());
		
		user_info_headimage.setOnClickListener(this);
		user_info_headimage1.setOnClickListener(this);
		user_info_username.setOnClickListener(this);
		user_info_address.setOnClickListener(this);
		user_info_sex.setOnClickListener(this);
		user_info_email.setOnClickListener(this);
		user_info_phone.setOnClickListener(this);
		user_info_userage.setOnClickListener(this);
		user_info_signtext.setOnClickListener(this);
		loginout_user.setOnClickListener(this);
		load_user_info_headimage();
	}
	

	@Override
	public void onResume() {
		super.onResume();
		String userinfo = userCache.getAsString("user");
		if(userinfo == null){
			Intent it =  new Intent(getActivity().getApplicationContext(),MainActivity.class);
			startActivity(it);
			return;
		}
		JsonObject userinfoJson =  new JsonParser().parse(userinfo).getAsJsonObject();
		
		user_info_username.setText(userinfoJson.get("name").getAsString());
		user_info_address.setText(userinfoJson.get("address").getAsString());
		user_info_sex.setText(userinfoJson.get("sex").getAsString());
		
		user_info_userage.setText(userinfoJson.get("age").getAsString());
		user_info_email.setText(userinfoJson.get("email").getAsString());
		user_info_phone.setText(userinfoJson.get("phoneNumber").getAsString());
		user_info_signtext.setText(userinfoJson.get("signtext").getAsString());
		load_user_info_headimage();
	}

	@Override
	public void onPause() {
		super.onPause();
	}
	
	
	private void load_user_info_headimage() {
		String userinfo = userCache.getAsString("user");
		if(userinfo != null){
			JsonObject userinfoJson =  new JsonParser().parse(userinfo).getAsJsonObject();
			String avatarurl = userinfoJson.get("avatarurl").getAsString();
			if(avatarurl != "" && avatarurl !=null){
				Log.e("Conf.BASE_URL+avatarurl",Conf.BASE_URL+avatarurl);
				Ion.with(getActivity()).load(Conf.BASE_URL+avatarurl).asBitmap()
				.setCallback(new FutureCallback<Bitmap>() {
					@Override
					public void onCompleted(Exception e, Bitmap bitmap) {
						userCache.remove("user_head_pic");
						userCache.put("user_head_pic", bitmap);
						user_info_headimage.setImageBitmap(bitmap);
					}
				});
				return;	
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.loginout_user:
			userCache.clear();
			Intent it = new Intent(getActivity(), MainActivity.class);
			startActivity(it); 
			break;
		case R.id.user_info_headimage :
			Intent it8 = new Intent(getActivity(), UploadUserHeadActivity.class);
			startActivity(it8); 
			break;
		case R.id.user_info_headimage1 :
			Intent it9 = new Intent(getActivity(), UploadUserHeadActivity.class);
			startActivity(it9); 
			break;
		case R.id.user_info_username:
			Intent it5 = new Intent(getActivity(), UpdateUserinfoActivity.class);
			it5.putExtra("setTitle", "修改名字");
			it5.putExtra("attr", "name");
			startActivity(it5); 
			break;
		case R.id.user_info_address:
			Intent it6 = new Intent(getActivity(), UpdateUserinfoActivity.class);
			it6.putExtra("setTitle", "修改地址");
			it6.putExtra("attr", "address");
			startActivity(it6); 
			break;
		case R.id.user_info_sex:
			Intent it7 = new Intent(getActivity(), UpdateUserinfoActivity.class);
			it7.putExtra("setTitle", "修改性别");
			it7.putExtra("attr", "sex");
			startActivity(it7); 
			break;
		case R.id.user_info_email:
			Intent it1 = new Intent(getActivity(), UpdateUserinfoActivity.class);
			it1.putExtra("setTitle", "修改邮箱");
			it1.putExtra("attr", "email");
			startActivity(it1); 
			break;
		case R.id.user_info_phone:
			Intent it2 = new Intent(getActivity(), UpdateUserinfoActivity.class);
			it2.putExtra("setTitle", "修改手机");
			it2.putExtra("attr", "phoneNumber");
			startActivity(it2); 
			break;
		case R.id.user_info_userage:
			Intent it3 = new Intent(getActivity(), UpdateUserinfoActivity.class);
			it3.putExtra("setTitle", "修改年龄");
			it3.putExtra("attr", "age");
			startActivity(it3); 
			break;
		case R.id.user_info_signtext:
			Intent it4 = new Intent(getActivity(), UpdateUserinfoActivity.class);
			it4.putExtra("setTitle", "修改签名");
			it4.putExtra("attr", "signtext");
			startActivity(it4); 
			break;
			
		  default:
	        	break;
		}

	}
	

}
